package com.ry.redis.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ry.bean.People;
import com.ry.redis.utils.RedisService;
import com.ry.redis.utils.RedisUtils;

@RestController
@RequestMapping("/redis")
public class RedisTestController {

	@Autowired
	private RedisTemplate<String, Object> redisTemplate;
	
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	
//	@Autowired
//	private RedisTemplate<Object, Object> redisTemplate;
	
	@Autowired
	private RedisService redisService;
	
	@GetMapping("/testConfig")
	public String testConfig() {
		
		redisTemplate.opsForValue().set("test4", "哈哈哈");
		System.out.println(redisTemplate.opsForValue().get("test4"));
		
		return "";
	}
	
	@GetMapping("/testObject")
	public String testObject() {
		
		People people = new People();
		people.setId(1);
		people.setName("张三");
		people.setAge(30);
		
		redisTemplate.opsForValue().set("people", JSON.toJSONString(people));
		String result = (String) redisTemplate.opsForValue().get("people");
		System.out.println(result);
		People p = JSONObject.parseObject(result, People.class);
		System.out.println(p.getId() + " " + p.getName() + " " + p.getAge());
		
		return "";
	}
	
	@GetMapping("/testHash")
	public String testHash() {

		Map<String, Object> map = new HashMap<>();
		map.put("number", 1);
		map.put("date", new Date());
		map.put("username", "李四");
		
		redisTemplate.opsForHash().putAll("test_hash", map);
		
		Map<String, Object> mapString = new HashMap<>();
		mapString.put("number", 1);
		mapString.put("date", new Date());
		mapString.put("username", "李四");
		redisService.hmset("test_hash_string", mapString);
		
		Set<Object> keys = redisTemplate.opsForHash().keys("test_hash");
		keys.forEach(key -> {
			Object o = redisTemplate.opsForHash().get("test_hash", key);
			System.out.println(o);
		});
		
		System.out.println("-----entries------");
		Map<Object, Object> map2 = redisTemplate.opsForHash().entries("test_hash");
		for (Map.Entry<Object, Object> m : map2.entrySet()) {
	        System.out.println("key:" + m.getKey() + " value:" + m.getValue());
	    }
		
		System.out.println("------RedisUtils-------");
		Map<Object, Object> map3 = RedisUtils.hmget("test_hash");
		for (Map.Entry<Object, Object> m : map3.entrySet()) {
	        System.out.println("key:" + m.getKey() + " value:" + m.getValue());
	    }
		
		System.out.println("------RedisUtils-------");
		Map<Object, Object> map4 = redisService.hmget("test_hash");
		for (Map.Entry<Object, Object> m : map4.entrySet()) {
	        System.out.println("key:" + m.getKey() + " value:" + m.getValue());
	    }
		
		System.out.println("---------stringRedisTemplate-----");
		Map<Object, Object> map5 = stringRedisTemplate.opsForHash().entries("test_hash_string");
		for (Map.Entry<Object, Object> m : map5.entrySet()) {
	        System.out.println("key:" + m.getKey() + " value:" + m.getValue());
	    }
		
		return "";
	}
	
}
